/* 
    Squirrel open project
    Copyright (C) 2012  Elias Aran Paul Farhan
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <squirrel/game.hpp>
Game::Game()
{
screen_pos = std::make_pair(0, 0);
}
Game::~Game()
{
	delete player;
	for(std::vector<GameObject*>::iterator it = objects.begin();it != objects.end(); it++)
	{
		delete *it;
	}
}
int Game::init(std::string xmlfilename)
{
	//Background init
	unsigned int width = 0;
	unsigned int height = 0;
	std::string filename = "arclesia/bateau/coque.png";
	GLuint image = this->importImg(filename, &width, &height);
	GameObject* background = new GameObject(filename, std::make_pair(0, 0), std::make_pair(width, height));
	if(image == 0)
		fprintf(stderr, "Error with loading %s\n", filename.c_str());
	background->setImage(image);
	background->setSize(std::make_pair(2250, 637));
	background->addBox(new Box(std::make_pair(706, 180), std::make_pair(1658, 230)));
	background->addBox(new Box(std::make_pair(583, 470), std::make_pair(1787, 550)));
	objects.push_back(background);
	
	//player init
	width = 0;
	height = 0;
	filename = "arclesia/soldat1/soldat1";
	
	std::vector<GLuint> imgs;
	for (int i = 1; i < 3; i++)
	{
		for (int j = 1; j < 5; j++)
		{
			for ( int k = 1; k < 5; k++)
			{
				std::stringstream sstm;
				sstm<<filename<<i<<j<<k<<".png";
				imgs.push_back(importImg(sstm.str(), &width, &height));
			}
		}
	}
	player = new Character(filename, std::make_pair(1000, 150), std::make_pair(width, height));
	player->setImage(imgs);
	player->setSizebyRatio(6);
	int frames[4] = {4, 4, 4, 4};
	player->addState("WALK", 3, frames, 0);
	player->addBox(new Box(std::make_pair(105, 100), std::make_pair(157, 153)), 0);
	player->addState("RUN", 10, frames, 16);
	player->addBox(new Box(std::make_pair(105, 100), std::make_pair(157, 153)), 1);
	player->addBox(new Box(std::make_pair(0,0), player->getSize()));

	return 0;
}
void Game::addplayer(Character* c)
{
	*player = *c;
}
void Game::manageKey(bool keys[4])
{
	player->setDirection(keys);
}
void Game::updateScene()
{

	screen_pos = std::make_pair(player->getPosition().first + player->getSize().first/2 - screen_size.first/2 , 
								player->getPosition().second + player->getSize().second/2 - screen_size.second/2);
	std::vector<Box*> nearbox;
	Box* checkbox = player->getCheckBox();
	for(std::vector<GameObject*>::iterator it = objects.begin();it != objects.end(); it++)
	{
		for(std::vector<Box*>::iterator itbox = (*it)->getBoxes()->begin(); itbox !=(*it)->getBoxes()->end(); itbox++)
		{
			if((*itbox)->BoxIsIn(checkbox))
			{
				std::cout<<"Box : ("<<(*itbox)->getOrigin().first<<", "<<(*itbox)->getOrigin().second<<
				"),("<<(*itbox)->getEnd().first<<", "<<(*itbox)->getEnd().second<<") in Box : ("<<checkbox->getOrigin().first<<", "<<
				checkbox->getOrigin().second<<"), ("<<checkbox->getEnd().first<<", "<<
				checkbox->getEnd().second<<")\n";
				nearbox.push_back(*itbox);
			}
		}
	}
	player->update(nearbox);
	

}
void Game::drawScene()
{
	
	for(std::vector<GameObject*>::iterator it = objects.begin();it != objects.end(); it++)
	{
		
		Point backpos = std::make_pair((*it)->getPosition().first - screen_pos.first, 
									   (*it)->getPosition().second - screen_pos.second);

		(*it)->drawObject(backpos);
	}
	Point player_pos = std::make_pair(screen_size.first/2 - player->getSize().first/2, 
									  screen_size.second/2 - player->getSize().second/2);

	player->drawObject(player_pos);
}
GLuint Game::importImg(std::string filename, unsigned int* width, unsigned int* height)
{
	std::string full_filename = "./data/sprites/"+filename;

	FILE* file = NULL;
	file = fopen(full_filename.c_str(), "rb");
	if (file == NULL)
	{
		fprintf(stderr, "Error: %s file not found\n", full_filename.c_str());
		return 0;
	}
	GLuint image = GLLoadPng(file, width, height);
	fclose(file);
	return image;
	
}
void Game::setScreenSize(Point screen_size)
{
	this->screen_size = screen_size;
}
